package com.honghu.villagepost.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.honghu.villagepost.R;
import com.honghu.villagepost.base.BaseActivity;
import com.honghu.villagepost.constant.IntentKeyConstant;
import com.honghu.villagepost.constant.IntentKeyConstant;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_again_pwd)
    EditText mEtAgainPwd;
    @BindView(R.id.confirm_tv)
    TextView mConfirmTv;
    private String mPhone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("用户注册");
        mPhone = getIntent().getStringExtra(IntentKeyConstant.PHONE);
        getCode();

    }

    @OnClick({R.id.tv_get_code, R.id.confirm_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.confirm_tv:
                String code = mEtCode.getText().toString().trim();
                String name = mEtName.getText().toString().trim();
                String pwd = mEtPwd.getText().toString().trim();
                String pwdAgain = mEtAgainPwd.getText().toString().trim();
                if (TextUtils.isEmpty(code)){
                    showToast("请输入验证码");
                    return;
                }
                if (TextUtils.isEmpty(name)){
                    showToast("请输入用户名");
                    return;
                }
                if (TextUtils.isEmpty(pwd)){
                    showToast("请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(pwdAgain)){
                    showToast("请再次输入密码");
                    return;
                }
                if (!pwd.equals(pwdAgain)){
                    showToast("两次密码不一致");
                    return;
                }

//                ApiService.register(this, account, code, pwd, new DialogCallback<LzyResponse<Object>>(this) {
//                    @Override
//                    public void onSuccess(Response<LzyResponse<Object>> response) {
//                        showToast(response.body().msg);
//                        finish();
//                    }
//                });
                break;
                default:
        }
    }


    private void getCode() {
//        ApiService.verifycode(this, mPhone, 1, new DialogCallback<LzyResponse<Object>>(this) {
//            @Override
//            public void onSuccess(Response<LzyResponse<Object>> response) {
//                mTvGetCode.setClickable(false);
//                mTvGetCode.setText(expired_time + "秒");
//                mHandler.postDelayed(runnable, 1000);
//
//            }
//        });

    }


    private Handler mHandler = new Handler();
    private int expired_time = 60;

    /**
     * 60秒倒计时
     */
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            expired_time--;
            mTvGetCode.setText(expired_time + "秒");
            if (expired_time == 0) {
                initVcode();
                return;
            }
            mHandler.postDelayed(this,1000);
        }
    };

    /*
     * 60秒过后初始化各状态
     */
    private void initVcode(){
        mTvGetCode.setClickable(true);
        mTvGetCode.setText("获取验证码");
    }

}
